﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bunian.Web.Common.Code;
using Bunian.Core.BusinessObjects;
using Bunian.Service;
using System.Web.UI.HtmlControls;

namespace Bunian.Web.Controls
{
    public partial class CFamilyProfile : System.Web.UI.UserControl
    {
        public Family CurrentFamily { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            BindControls();
        }

        protected void BindControls()
        {
            ltFamilyName.Text = CurrentFamily.ContactDetails.FullName;

            ltFamilyAddress.Text = CurrentFamily.ContactDetails.GetAddressByType(AddressType.StreetAddress);
            ltDeficiency.Text = Convert.ToInt32(Math.Floor(CurrentFamily.Deficiency)).ToString(); ;

            ltNumOfChildrenFamily.Text = BeneficiaryFormattingHelper.GetFamilyInfo(CurrentFamily);

            //ltCoveredCountFamily.Text = BeneficiaryFormattingHelper.GetCoveredCount(CurrentFamily);

            ltProviderFamily.Text = BeneficiaryFormattingHelper.GetProvider(CurrentFamily);
            
            ltSourcesOfIncome.Text = CurrentFamily.SourcesOfIncome;

            ltFatherName.Text = (null != CurrentFamily.Father) ? CurrentFamily.Father.FullName : "No Father Info";

            ltDeceased.Text = getDeceasedLabel(CurrentFamily.Father);

            ltMother.Text = (null != CurrentFamily.Mother) ? CurrentFamily.Mother.FullName : "No Mother Info";

            if (null != CurrentFamily.FamilyMembers)
            {
                rptFamilyMembers.ItemDataBound += new RepeaterItemEventHandler(rptFamilyMembers_ItemDataBound);

                rptFamilyMembers.DataSource = CurrentFamily.FamilyMembers;
                rptFamilyMembers.DataBind();
            }           
        }

        protected void rptFamilyMembers_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            switch (e.Item.ItemType)
            {
                case ListItemType.AlternatingItem:
                case ListItemType.Item:

                    Child currentChild = (Child)e.Item.DataItem;

                    Literal ltChildName = (Literal)e.Item.FindControl("ltChildName");

                    ltChildName.Text = BeneficiaryFormattingHelper.GetChildDescription(currentChild);
                    
                    HtmlAnchor aChildProfile = (HtmlAnchor)e.Item.FindControl("aChildProfile");
                    aChildProfile.HRef = "~/pages/profile.individual.aspx?cid=" + currentChild.Id;

                    break;
            }
        }

        private string getDeceasedLabel(Contact person)
        {
            if (null != person)
            {
                if (person.IsDeceased)
                {
                    return "(deceased)";
                }
                else
                {
                    return string.Empty;
                }
            }
            else
            {
                return string.Empty;
            }
        }
    }
}